package com.xx.doublexchat.common.chat.consumer;

import com.xx.doublexchat.common.chat.dao.ContactDao;
import com.xx.doublexchat.common.chat.dao.MessageDao;
import com.xx.doublexchat.common.chat.dao.RoomDao;
import com.xx.doublexchat.common.chat.dao.RoomFriendDao;
import com.xx.doublexchat.common.chat.domain.dto.MsgSendMessageDTO;
import com.xx.doublexchat.common.chat.domain.entity.Message;
import com.xx.doublexchat.common.chat.domain.entity.Room;
import com.xx.doublexchat.common.chat.domain.entity.RoomFriend;
import com.xx.doublexchat.common.chat.domain.enums.RoomTypeEnum;
import com.xx.doublexchat.common.chat.domain.vo.response.ChatMessageResponse;
import com.xx.doublexchat.common.chat.service.ChatService;
import com.xx.doublexchat.common.chat.service.IPushService;
import com.xx.doublexchat.common.chat.service.cache.GroupMemberCache;
import com.xx.doublexchat.common.chat.service.cache.HotRoomCache;
import com.xx.doublexchat.common.chat.service.cache.RoomCache;
import com.xx.doublexchat.common.websocket.service.adapter.WebSocketAdapter;
import constant.MQConstant;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 消息推送mq消费者
 */
@RocketMQMessageListener(consumerGroup = MQConstant.SEND_MSG_GROUP, topic = MQConstant.SEND_MSG_TOPIC)
@Component
public class MsgSendConsumer implements RocketMQListener<MsgSendMessageDTO> {

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private RoomCache roomCache;

    @Autowired
    private ChatService chatService;

    @Autowired
    private RoomDao roomDao;

    @Autowired
    private GroupMemberCache groupMemberCache;

    @Autowired
    private RoomFriendDao roomFriendDao;

    @Autowired
    private ContactDao contactDao;

    @Autowired
    private IPushService pushService;

    @Autowired
    private HotRoomCache hotRoomCache;

    @Override
    public void onMessage(MsgSendMessageDTO dto) {
        //获取消息详情
        Message message = messageDao.getById(dto.getMsgId());
        //获取消息的详细信息
        ChatMessageResponse msgResp = chatService.getMsgResp(message, null);
        //获取房间信息
        Room room = roomCache.get(message.getRoomId());
        //刷新房间的最新时间
        roomDao.refreshActiveTime(room.getId(), message.getId(), message.getCreateTime());
        //删除当前房间的缓存 ----> 让用户主动拉取最新缓存
        roomCache.delete(room.getId());
        //判断是否为热点群聊 ----> 热点群聊采用热点信箱的模式
        if (room.isHotRoom()) {
            //更新热门群聊时间-redis
            hotRoomCache.refreshActiveTime(room.getId(), message.getCreateTime());
            //推送所有人
            pushService.sendPushMsg(WebSocketAdapter.buildMsgSend(msgResp));
        } else {
            List<Long> sendUidList = new ArrayList<>();
            //群聊
            if (Objects.equals(room.getType(), RoomTypeEnum.GROUP.getType())) {
                //成员ID集合
                sendUidList = groupMemberCache.getMemberUidList(room.getId());
            } else if (Objects.equals(room.getType(), RoomTypeEnum.FRIEND.getType())) {
                //单聊 --> 查询好友房间表
                RoomFriend roomFriend = roomFriendDao.getByRoomId(room.getId());
                sendUidList = Arrays.asList(roomFriend.getUid1(), roomFriend.getUid2());
            }
            //更新所有群成员的会话时间
            contactDao.refreshOrCreateActiveTime(room.getId(), sendUidList, message.getId(), message.getCreateTime());
            //推送房间成员
            pushService.sendPushMsg(WebSocketAdapter.buildMsgSend(msgResp), sendUidList);
        }
    }
}
